<?php

/**
 * Description of Notifications
 *
 * @author ghanemel
 */
class notifications extends CI_Controller {

    public function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in_user')) {
            $this->logged_in = TRUE;
        } else {
            redirect('Login/', 'refresh');
        }
        $this->load->model('Dao');
        $this->load->helper('date');
    }

    public function whatsUp($notifsNumber) {

        if (!isset($notifsNumber)) {
            echo "0";
            return;
        }

        if (!is_numeric($notifsNumber)) {
            echo "not numeric!!" . $notifsNumber;
            $notifsNumber = 0;
            return;
        }

        $user = $this->session->userdata('logged_in_user');
        $time = time();
        $resp = "0";
        $data = Array();
        while (time() - $time < (59 * 5)) { //abandon après 5 min
            $notifs = $this->Dao->findNotifsNbr($user->id);
            if ($notifs != $notifsNumber) {
                $notifsNumber = $notifs;
                $resp = $notifs;
                $data['ntfByUser'] = $this->Dao->findNotifsNbrByUser($user->id);
                break;
            } else {
                sleep(1); // 1 Second
            }
        }
        $data['notifsNumber'] = $resp;
        echo json_encode($data);
    }

}

?>
